<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>通过子文档查询父文档 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/has-child.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/has-child.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/has-child.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/has-child.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">通过子文档查询父文档</span>
</div>
<div class="navheader">
<span class="prev">
<a href="indexing-parent-child.html">« 构建父-子文档索引</a>
</span>
<span class="next">
<a href="has-parent.html">通过父文档查询子文档 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="has-child"></a>通过子文档查询父文档 (has_child)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/50_Has_child.asciidoc">edit</a>
</h2>
</div></div></div>
<p><code class="literal">has_child</code>查询和过滤可以通过子文档的内容来查询父文档。例如，我们根据如下查询，可查出所有80后员工所在的分公司：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/branch/_search
{
  "query": {
    "has_child": {
      "type": "employee",
      "query": {
        "range": {
          "dob": {
            "gte": "1980-01-01"
          }
        }
      }
    }
  }
}</pre>
</div>
<p>类似于<a class="xref" href="nested-query.html" title="嵌套对象查询"><code class="literal">nested</code>查询</a>，<code class="literal">has_child</code>查询可以匹配多个子文档，并且每一个子文档的评分都不同。但是由于每一个子文档都带有评分，这些评分如何规约成父文档的总得分取决于参数<code class="literal">score_mode</code>。该参数有多种取值策略：默认为 <code class="literal">none</code>，会忽略子文档的评分，并且会给父文档评分设置为 <code class="literal">1.0</code> ；
除此以外还可以设置成<code class="literal">avg</code>、<code class="literal">min</code>、<code class="literal">max</code>和<code class="literal">sum</code>。</p>

<p>下面的查询将会同时返回<code class="word">london</code>和<code class="word">liverpool</code>，不过由于<code class="word">Alice Smith</code>要比<code class="word">Barry Smith</code>更加匹配查询条件，因此<code class="word">london</code>会得到一个更高的评分。</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/branch/_search
{
  "query": {
    "has_child": {
      "type":       "employee",
      "score_mode": "max",
      "query": {
        "match": {
          "name": "Alice Smith"
        }
      }
    }
  }
}</pre>
</div>
<div class="tip admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">score_mode</code>为默认的<code class="literal">none</code>时，会比其他模式快得多，这是因为Elasticsearch不需要计算每一个子文档的评分。只有当你真正需要关心评分结果时，才需要设置<code class="literal">score_mode</code>的值为<code class="literal">avg</code>、<code class="literal">min</code>、<code class="literal">max</code>或<code class="literal">sum</code> 。</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="min-max-children"></a>参数 min_children 和 max_children <a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/50_Has_child.asciidoc">edit</a>
</h3>
</div></div></div>
<p><code class="literal">has_child</code>查询和过滤都可以接受这两个参数：<code class="literal">min_children</code>和<code class="literal">max_children</code>。使用这两个参数时，只有当子文档数量在指定范围内时，才会返回父文档。</p>
<p>如下查询只会返回至少有两个雇员的分公司：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /company/branch/_search
{
  "query": {
    "has_child": {
      "type":         "employee",
      "min_children": 2, <a id="CO280-1"></a><i class="conum" data-value="1"></i>
      "query": {
        "match_all": {}
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO280-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>至少有两个雇员的分公司才会符合查询条件。</p>
</td>
</tr>
</table>
</div>
<p>带有<code class="literal">min_children</code>和<code class="literal">max_children</code>参数的<code class="literal">has_child</code>查询或过滤，和启用评分计算的<code class="literal">has_child</code>查询的性能非常接近。</p>
<div class="sidebar">
<div class="titlepage"><div><div>
<p class="title"><strong>has_child 过滤</strong></p>
</div></div></div>
<p><code class="literal">has_child</code>查询和过滤在运行机制上类似，区别是<code class="literal">has_child</code>过滤不支持<code class="literal">score_mode</code>参数。<code class="literal">has_child</code>过滤仅用于筛选内容（如内部的一个<code class="literal">filtered</code>查询），和其他过滤行为类似：包含或者排除，但不会进行评分。</p>

<p><code class="literal">has_child</code>过滤的结果不会被缓存，但是<code class="literal">has_child</code>过滤内部的过滤适用于通常的缓存规则。</p>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="indexing-parent-child.html">« 构建父-子文档索引</a>
</span>
<span class="next">
<a href="has-parent.html">通过父文档查询子文档 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>